package nahuel.games.carracer;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;

public class Ambiente{
    private int velRelativaY = 0;
    private Bitmap bmp;
    private int currentFrame;
	private int altoD;
	private int anchoD;
    private int width;
    private int height = 0;
    Rect src = new Rect();
    Rect dst = new Rect();

	static final String FRAME = "frame";

	public Ambiente(Bitmap bmp, int anchoD, int altoD) {
		this.altoD = altoD;
		this.anchoD = anchoD;
		this.bmp = bmp;
		this.width = bmp.getWidth();
		float relacion = width * anchoD / altoD;
		this.height = Math.round(relacion);   //Ajusto la altura de la pista a la pantalla
		currentFrame = bmp.getHeight() - height;
	}

	public void update() {
		if (currentFrame <= 0) {
			currentFrame = bmp.getHeight() - height;
		}
		currentFrame -= velRelativaY;
   }

    public void draw(Canvas canvas) {
		int srcX = 0;
		int srcY = currentFrame;
		src.set(srcX, srcY, srcX + width, srcY + height);
		dst.set(0, 0, anchoD, altoD);
		canvas.drawBitmap(bmp, src, dst, null);
    }

    public void setVelocidadRelativa(int y){
		this.velRelativaY = y;
    }

	public Bundle getBundle() {
		Bundle bund = new Bundle();
		bund.putInt(FRAME, currentFrame);
		return bund;
	}
	
	public void setBundle(Bundle bundle) {
		currentFrame = bundle.getInt(FRAME);
	}

}
